###
 # @Author: didiplus
 # @Date: 2025-01-22 09:10:26
 # @LastEditTime: 2025-01-22 09:31:41
 # @LastEditors: didiplus
 # @Description: 
 # @FilePath: \pythonscript\shell\monitor_process.sh
### 


#!/bin/bash

# 定义要监控的进程名称
process_name="your_process_name"

# 定义启动进程的命令
start_command="your_start_command"

# 定义日志文件路径
log_file="monitor_process.log"

while true; do
    # 检查进程是否在运行
    if pgrep -x "$process_name" > /dev/null; then
        echo "$(date +"%Y-%m-%d %H:%M:%S") - 进程 $process_name 正在运行" >> "$log_file"
    else
        echo "$(date +"%Y-%m-%d %H:%M:%S") - 进程 $process_name 未运行，尝试启动..." >> "$log_file"
        $start_command
        if pgrep -x "$process_name" > /dev/null; then
            echo "$(date +"%Y-%m-%d %H:%M:%S") - 进程 $process_name 启动成功" >> "$log_file"
        else
            echo "$(date +"%Y-%m-%d %H:%M:%S") - 进程 $process_name 启动失败" >> "$log_file"
        fi
    fi
    # 每隔一段时间检查一次进程状态
    sleep 60
done
